package org.xx.armory.swing.builders;

import org.xx.armory.objectparser.ParsedObjectBuilder;

import javax.swing.*;

import static org.apache.commons.lang3.StringUtils.left;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class ActionModelBuilder
        extends ParsedObjectBuilder<ActionModel> {
    private String name;
    private String title;
    private String mnemonic;
    private String accelerator;
    private String smallIcon;
    private String description;
    private String command;
    private boolean enabled;
    private  boolean causeVerification;

    public ActionModelBuilder() {
    }

    public ActionModelBuilder setName(
            String name
    ) {
        this.name = name;
        return this;
    }

    public ActionModelBuilder setTitle(
            String title
    ) {
        this.title = title;
        return this;
    }

    public ActionModelBuilder setMnemonic(
            String mnemonic
    ) {
        this.mnemonic = mnemonic;
        return this;
    }

    public ActionModelBuilder setAccelerator(
            String accelerator
    ) {
        this.accelerator = accelerator;
        return this;
    }

    public ActionModelBuilder setSmallIcon(
            String smallIcon
    ) {
        this.smallIcon = smallIcon;
        return this;
    }

    public ActionModelBuilder setDescription(
            String description
    ) {
        this.description = description;
        return this;
    }

    public ActionModelBuilder setCommand(
            String command
    ) {
        this.command = command;
        return this;
    }

    public ActionModelBuilder setEnabled(
            boolean enabled
    ) {
        this.enabled = enabled;
        return this;
    }

    public ActionModelBuilder setCauseVerification(
            boolean causeVerification
    ) {
        this.causeVerification = causeVerification;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionModel build() {
        this.name = trimToEmpty(this.name);
        this.title = trimToEmpty(this.title);
        this.mnemonic = left(trimToEmpty(this.mnemonic), 1);
        this.accelerator = trimToEmpty(this.accelerator);
        this.smallIcon = trimToEmpty(this.smallIcon);
        this.description = trimToEmpty(this.description);
        this.command = trimToEmpty(this.command);

        return new ActionModel(getLineInfo(), name, title,
                               !mnemonic.isEmpty() ? mnemonic.charAt(0) : '\0',
                               !accelerator.isEmpty() ? KeyStroke.getKeyStroke(accelerator) : null,
                               smallIcon,
                               description, command, enabled, causeVerification);
    }
}
